# _*_ coding:utf-8 _*_
# By:赵先宇
import traceback
from .base import BasePlugin
from lib.response import BaseResponse
from lib.logger import logger


class Basic(BasePlugin):

    def os_platform(self, handler, hostname):  # 获取系统平台
        output = handler.cmd('uname', hostname)

    def os_version(self, handler, hostname):
        """
        获取系统版本
        :param handler:
        :param hostname:
        :return:
        """
        output = handler.cmd('cat /etc/issue', hostname)
        result = output.strip().split('\n')[0]
        return result

    def os_hostname(self, handler, hostname):
        """
        获取主机名
        :param handler:
        :param hostname:
        :return:
        """
        output = handler.cmd('hostname', hostname)
        return output.strip()

    def win(self, handler, hostname):
        raise NotImplementedError('win方法必须被实现')

    def linux(self, handler, hostname):
        response = BaseResponse()
        try:
            if self.debug:
                ret = {
                    'os_platform': 'linux',
                    'os_version': '7.0',
                    'hostname': 'c1.com',
                }
            else:
                ret = {
                    'os_platform': self.os_platform(handler, hostname),
                    'os_version': self.os_version(handler, hostname),
                    'hostname': self.os_hostname(handler, hostname),
                }
            response.data = ret
        except Exception as e:
            error_msg = traceback.format_exc()
            response.status = False
            response.error = error_msg
            logger.error(error_msg)  # 错误信息记录日志
        return response.dict
